package com.aliyun.messagemgt.common.dto.response;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import lombok.Getter;

/**
 * Created with IDEA
 * 所有controller返回值均要用此类
 * 新增
 * 1.success() 返回值优化
 * 2.success(Object datas) 方法分页参数自获取
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/3/15
 * @time:15:41
 */
@Getter
public class AjaxResult {

    /**
     * 默认成功的缓存实例
     */
    private static final AjaxResult OPERATION_SUCCESS = success(null);

    public static AjaxResult success() {
        return OPERATION_SUCCESS;
    }

    public static AjaxResult success(Object datas) {
        AjaxResult ajaxResult = new AjaxResult().setSuccess(true)
                .setErrCode(0)
                .setErrMsg("success")
                .setErrName(null)
                .setDatas(datas);
        // 如果是Page类型，默认附带上分页等一系列参数
        if (datas instanceof Page) {
            Page page = (Page) datas;
            ajaxResult.setPageSize(page.getPageSize())
                    .setPageNum(page.getPageNum())
                    .setPages(page.getPages())
                    .setTotalCount((int) page.getTotal());
        }
        return ajaxResult;
    }

    public static AjaxResult fail(int errCode) {
        return fail(errCode, null, null, null);
    }

    public static AjaxResult fail(int errCode, String errMsg, String errName, String datas) {
        return new AjaxResult()
                .setSuccess(false)
                .setErrCode(errCode)
                .setErrMsg(errMsg)
                .setErrName(errName)
                .setDatas(datas);
    }


    private boolean success;
    private int errCode;
    private String errMsg;
    private String errName;
    private int totalCount;
    private Object datas;
    private int pageNum;
    private int pageSize;
    private int  pages;

    private AjaxResult() {
        this.success = true;
        this.errCode = 0;
        this.errMsg = "success";
        this.errName = "";
        this.totalCount = 0;
        this.datas = null;
        this.pageNum = 1;
        this.pageSize = 10;
        this.pages = 1;
    }

    public AjaxResult setPages(int pages) {
        this.pages = pages;
        return this;
    }


    public AjaxResult setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public AjaxResult setErrCode(int errCode) {
        this.errCode = errCode;
        return this;
    }

    public AjaxResult setErrMsg(String errMsg) {
        this.errMsg = errMsg;
        return this;
    }

    public AjaxResult setErrName(String errName) {
        this.errName = errName;
        return this;
    }

    public AjaxResult setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        return this;
    }

    public AjaxResult setDatas(Object datas) {
        this.datas = datas;
        return this;
    }

    public AjaxResult setPageNum(int pageNum) {
        this.pageNum = pageNum;
        return this;
    }

    public AjaxResult setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
